<?php

include('../includes/smarty.php');
include_once('../config.php');

$ADMIN = new Admin();

session_start();

if(isset($_SESSION["id"]))
{
	//	Validate the session.
	$row = $ADMIN->validate_admin_session($_SESSION["id"]);
	
	if($row)
	{
		//	Change the lastvisit date, and show the admin screen.
		$ADMIN->update_lastvisit($_SESSION["id"]);
		
		$smarty->assign('title', 'Admin Home');
		$smarty->assign('path', $site);
		$smarty->assign('body', 'main/admin/home.tpl');

		$smarty->display('admin.tpl');
	}
	else
	{
		//	Kill the current session, and show the login screen.
		session_destroy();
		
		$smarty->assign('title', 'Admin');
		$smarty->assign('path', $site);
		$smarty->assign('forgotPassword', 'forgotPassword.php');

		$smarty->display('adminLogin.tpl');
	}
}
else if(empty($_POST))
{	
	$smarty->assign('title', 'Admin');
	$smarty->assign('path', $site);
	$smarty->assign('forgotPassword', 'forgotPassword.php');
	$smarty->assign('error', '');

	$smarty->display('adminLogin.tpl');	
}
else
{
	$user = $_POST["username"];
	$pass = $_POST["password"];
	
	$row = $ADMIN->admin_login($user, $pass);
	
	if($row)
	{		
		$_SESSION['id'] = $row['admin_id'];
		$_SESSION['fName'] = $row['fname'];
		$_SESSION['lName'] = $row['lname'];
		
		$ADMIN->update_lastvisit($_SESSION['id']);
		header("Location: ../admin");
	}
	else
	{
		$smarty->assign('title', 'Admin');
		$smarty->assign('path', $site);
		$smarty->assign('forgotPassword', 'forgotPassword.php');
		$smarty->assign('error', 'The username and password entered was incorrect.');

		$smarty->display('adminLogin.tpl');	
	}
}

?>